//
//  RequestHttpNetWrok.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/5/27.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "RequestHttpNetWrok.h"

@implementation RequestHttpNetWrok


static RequestHttpNetWrok * operationManager;

+(RequestHttpNetWrok *)shareInstance{
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        operationManager = [[RequestHttpNetWrok alloc] initWithBaseURL:[NSURL URLWithString:DEF_INTERFACE_SERVICE]];
        operationManager.requestSerializer.timeoutInterval = 15;
        
        operationManager.requestSerializer = [AFHTTPRequestSerializer serializer];
        operationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        operationManager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", @"text/plain", nil];
    });
    
    return operationManager;
}


/**
 *  post请求
 */
+ (RequestHttpNetWrok *)requestPostWithParameters:(NSDictionary *)parameters
                                    urlString:(NSString *)urlString
                                 finishHandle:(successBlock)finishHandle
                                   failHandle:(failtureBlock)failHandle
{
    RequestHttpNetWrok *manager = [RequestHttpNetWrok shareInstance];
    
    NSLog(@"parameters %@manager.baseURL  %@%@", parameters,manager.baseURL,urlString);
    
    [manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
         // 这里可以获取到目前数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [RequestHttpNetWrok requestSuccess:responseObject finishHandle:finishHandle failHandle:(failtureBlock)failHandle];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [RequestHttpNetWrok requestError:error failHandle:failHandle];
    }];
    
    return manager;
}


/**
 *  get请求
 */
+ (RequestHttpNetWrok *)requestGettWithParameters:(NSDictionary *)parameters
                                        urlString:(NSString *)urlString
                                     finishHandle:(successBlock)finishHandle
                                       failHandle:(failtureBlock)failHandle
{
    RequestHttpNetWrok *manager = [RequestHttpNetWrok shareInstance];
    
    
//    NSLog(@"parameters %@manager.baseURL  %@%@", parameters,manager.baseURL,urlString);
    
    [manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
        
        [RequestHttpNetWrok requestSuccess:responseObject finishHandle:finishHandle failHandle:(failtureBlock)failHandle];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [RequestHttpNetWrok requestError:error failHandle:failHandle];
    }];
    
    return manager;
}

/**
 *  网络请求成功处理
 */
+ (void)requestSuccess:(id)responseObject finishHandle:(successBlock)finishHandle failHandle:(failtureBlock)failHandle{
    
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseObject
                          options:kNilOptions
                          error:nil];
    
    NSLog(@"json json  %@",json);
    
    NSLog(@"错误信息 %@",json[@"message"]);
    
    if ([json[@"errorcode"] integerValue] == 0) {
        //正确逻辑处理
        finishHandle(json);
    }else{
        //错误逻辑处理
        [MXUtility showTips:json[@"message"]];

        NSError *error = [NSError errorWithDomain:ERRORDAOMAIN code:[json[@"errorcode"] integerValue] userInfo:responseObject];
        failHandle(error);
    }
}


/**
 *  网络请求失败处理
 */
+ (void)requestError:(NSError *)error failHandle:(failtureBlock)failHandle{
    
    NSString *message;
    
    if(error.code == kCFURLErrorNotConnectedToInternet || error.code == kCFURLErrorCannotConnectToHost)
    {
        message = @"网络不可用，请检查网络连接";
    }else if (error.code == kCFURLErrorTimedOut)
    {
        message = @"网络请求超时";
    }else
    {
        message = @"请求失败，请稍后再试";
    }
    
    [MXUtility showTips:message];
    
    CC_LOG_VALUE(error.localizedDescription);
    
    failHandle(error);
}


@end
